/*
This software is distributed under the Simplified BSD License:

Copyright (c) 2008, Chris Venter <chris.venter@gmail.com>
All rights reserved.

Redistribution and use in source and binary forms, with or without modification, 
are permitted provided that the following conditions are met:

    * Redistributions of source code must retain the above copyright notice, 
    	this list of conditions and the following disclaimer.
    * Redistributions in binary form must reproduce the above copyright notice, 
    	this list of conditions and the following disclaimer in the documentation 
    	and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY 
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 
SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 
TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 
BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 
ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH 
DAMAGE.
*/
#ifndef GENERIK_GTK_INIT_H
#define GENERIK_GTK_INIT_H

/** \file init.h

Defines a couple of wrappers around the gtk gang of libraries.
Most of the wrappers make it easier to initialise things, and provides a cleaned up
interface to the classes.
*/

#include "macros.h"

namespace generik
{
	namespace g
	{
		/** A wrapper class which initialises the g_thread library upon instantation.
		It is used by the derived gdk::threads_init class.
		*/
		struct thread_init
		{
			thread_init()
			{
				GENERIK_TRACER;
				GENERIK_TRACE("--> g_thread_init(0)");
				::g_thread_init(0);
			}
		};
	}
		
	namespace gdk
	{
		
		/** A wrapper class which declares a recursive mutex for use by the 
		gdk locking functions. It is derived from g::thread_init so that
		the g_thread library is set up correctly before use.
		*/
		struct threads_init
		: g::thread_init
		{
			static GStaticRecMutex _mutex;
			static void enter()
			{
				REM_GENERIK_TRACER;
				g_static_rec_mutex_lock(&_mutex);
			}
			static void leave()
			{
				REM_GENERIK_TRACER;
				g_static_rec_mutex_unlock(&_mutex);
			}
			/** Constructor.
			This sets up gdk to use the static enter() method, so that
			gdk_threads_enter() can be called recursively from the same
			thread.
			*/
			threads_init()
			{
				GENERIK_TRACER;
				//g_static_rec_mutex_init(&_mutex);
				gdk_threads_set_lock_functions(enter, leave);
				GENERIK_TRACE("--> gdk_threads_init()");
				::gdk_threads_init();
			}
			/*
			~threads_init()
			{
				REM_GENERIK_TRACER;
				g_static_rec_mutex_free(&_mutex);
			}
			*/
		};
		
		/** A wrapper class which acts as a scoped guard for gdk.
		Upon construction this instance issues a gdk_threads_enter() call,
		and upon destruction issues the corresponding gdk_threads_leave() call.
		*/
		struct threads_enter
		{
			threads_enter()
			{
				REM_GENERIK_TRACER;
				GENERIK_TRACE("--> gdk_threads_enter() ?");
				::gdk_threads_enter();
			}
			~threads_enter()
			{
				REM_GENERIK_TRACER;
				GENERIK_TRACE("--> gdk_threads_leave() ?");
				::gdk_flush();
				::gdk_threads_leave();
			}
		};
		
	}
	
	namespace gtk
	{
	
		/** A wrapper class which initialises gtk, and makes sure that
		the required gdk initialisation has been done.
		*/
		struct init
		: gdk::threads_init
		{
			/** Constructor.
			Initialises gdk (since this class is derived from gdk::threads_init)
			and then initialises gtk.
			It requires that the commandline arguments be passed along to it.
			*/
			init(int& argc, char**& argv)
			{
				GENERIK_TRACER;
				GENERIK_TRACE("--> gtk_init(&argc, &argv)");
				::gtk_init(&argc, &argv);
			}
		};

		/** A wrapper class for the gtk_main() call.
		It logs its construction and destruction.
		*/
		struct main
		{
			main()
			{
				GENERIK_TRACER;
				GENERIK_TRACE("--> gtk_main()");
				::gtk_main();
			}
			~main()
			{
				GENERIK_TRACER;
			}
		};

		/** A wrapper class for the gtk_main_quit() call.
		*/
		struct quit
		{
			quit()
			{
				GENERIK_TRACER;
				GENERIK_TRACE("--> gtk_main_quit()");
				::gtk_main_quit();
			}
			~quit()
			{
				REM_GENERIK_TRACER;
			}
		};
				
	}
	
}

#endif



